<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Mine\MineModel;

/**
 * @property int $id 
 * @property int $uid 
 * @property int $main_uid 
 * @property int $corp_id 企业ID
 * @property int $group_id 分组ID
 * @property string $room_wxid 群ID
 * @property string $room_name 群名称
 * @property string $room_announcement 群公告
 * @property string $room_avatar_url 群头像
 * @property int $room_member_count 群成员数量
 * @property string $create_user_id 群主ID
 * @property int $room_flag 
 * @property int $room_team_type 团队类型
 * @property int $room_is_forbid_change_name 禁止修改名称：0=否 1=是
 * @property int $room_is_invite_confirmation 群聊邀请确认： 0=关闭 1=开启
 * @property string $source 来源
 * @property int $customer_room_type 客户群类型:0=企微群 1=外部微信群 2=客户群
 * @property int $create_time 群创建时间
 * @property string $room_owner_time 成为群主时间
 * @property string $room_owner_name 群主名称
 * @property int $is_saved 是否已保存通讯录：0=否 1=是
 * @property \Carbon\Carbon $created_at 首次同步时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $chat_id 客户群id
 * @property-read null|\Hyperf\Database\Model\Collection|ChRoomMember[] $roomMembers 
 * @property-read null|\Hyperf\Database\Model\Collection|ChRoomTag[] $roomTags 
 * @property-read null|ChRobot $robot 
 * @property-read null|\Hyperf\Database\Model\Collection|ChRobot[] $robots 
 * @property-read null|\Hyperf\Database\Model\Collection|ChRoomMember[] $admins 
 */
class ChRoom extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_room';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'uid', 'main_uid', 'corp_id', 'group_id', 'room_wxid', 'room_name', 'room_announcement', 'room_avatar_url', 'room_member_count', 'create_user_id', 'room_flag', 'room_team_type', 'room_is_forbid_change_name', 'room_is_invite_confirmation', 'source', 'customer_room_type', 'create_time', 'room_owner_time', 'room_owner_name', 'is_saved', 'created_at', 'updated_at', 'chat_id'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'uid' => 'integer', 'main_uid' => 'integer', 'corp_id' => 'integer', 'group_id' => 'integer', 'room_member_count' => 'integer', 'room_flag' => 'integer', 'room_team_type' => 'integer', 'room_is_forbid_change_name' => 'integer', 'room_is_invite_confirmation' => 'integer', 'customer_room_type' => 'integer', 'create_time' => 'integer', 'is_saved' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];


//    public function getRoomWxidAttribute($value): string
//    {
//        return (string) $value;// 确保为字符串类型,防止精度丢失
//    }

    /**
     * 群标签
     * @return BelongsToMany
     */
    public function roomTags(): BelongsToMany
    {
        return $this->belongsToMany(ChRoomTag::class, 'ch_room_tag_room', 'room_id', 'tag_id');
    }

    /**
     * 企微号
     * @deprecated
     * @return HasOne
     */
    public function robot(): HasOne
    {
        return $this->hasOne(ChRobot::class, 'id', 'robot_id');
    }

    /**
     * 机器人
     * @return BelongsToMany
     */
    public function robots(): BelongsToMany
    {
        return $this->belongsToMany(ChRobot::class, 'ch_room_robot', 'room_id', 'robot_id')->withPivot('is_saved')->withPivot('is_do_not_disturb');
    }

    /**
     * 管理员
     * @return HasMany
     */
    public function admins(): HasMany
    {
        return $this->hasMany(ChRoomMember::class,'room_wxid','room_wxid')->where('is_admin',1);
    }

    public function roomMembers(): HasMany
    {
        return $this->hasMany(ChRoomMember::class,'room_wxid','room_wxid');
    }

    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(ChRoomGroup::class, 'ch_room_group_map', 'room_id', 'group_id');
    }
}
